package com.jobs.producer0.demo;

import com.jobs.common.factory.RabbitMqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 发布确认模式，
 * 1、单个确认
 * 2、批量确认
 * 3、异步批量确认
 * @author xdh
 * */

public class ReceiveLogs01 {
        //交换机名称
        public static final String EXCHANGE_NAME = "logs";
        public static void main(String[] args) throws Exception {
            //3、异步批量确认
            // 发布1000个异步确认消息，耗时36ms
            ReceiveLogs01.publicMessageAsync();

        }

        public static void publicMessageAsync() throws Exception{
            long begin = System.currentTimeMillis();
            Channel channel = RabbitMqUtils.getChannel();
            // 声明一个队列，名称随机，当消费者断开与队列的连接时，队列自动删除
            String queueName =  channel.queueDeclare().getQueue();
            channel.queueBind(queueName, EXCHANGE_NAME, "");
            System.out.println("等待接收消息，把接收到的消息打印到屏幕上.....");
            DeliverCallback deliverCallback = new DeliverCallback() {
                @Override
                public void handle(String consumerTag, Delivery message) throws IOException {
                    System.out.println("ReceiveLogs01控制台打印接受到的消息：" + new String(message.getBody()));
                }
            };
            CancelCallback cancelCallback = consumerTag -> {
                System.out.println("CancelCallback：" + consumerTag);
            };
            channel.basicConsume(queueName, true, deliverCallback, cancelCallback);
        }
    }
